package com.mf.springcloud.controller;


import com.mf.springcloud.entity.Article;
import com.mf.springcloud.service.ArticleService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value="/article/get/{id}",method = RequestMethod.GET)
   @HystrixCommand(fallbackMethod = "processHystrix_Get")
    public Article get(@PathVariable("id") Integer id){
        Article article = articleService.get(id);
        if(article == null){
            throw  new RuntimeException("没有对应ID数据");
        }
      return article;
    }

    public Article processHystrix_Get(@PathVariable("id") Integer id){

        return new Article().setId(id).setDb("error").setDescription("数据库找不到对应数据");
    }

    @RequestMapping(value="/article/add",method = RequestMethod.POST)
    public boolean add(@RequestBody Article article){
        return articleService.addArticle(article);
    }

    @RequestMapping(value="/article/list",method = RequestMethod.GET)
    public List<Article> list(){
        return articleService.list();
    }


    @RequestMapping(value="/article/discovery",method = RequestMethod.GET)
    public Object discovery(){
        List<String> list = discoveryClient.getServices();
        List<ServiceInstance> serviceInstanceList=discoveryClient.getInstances("microservicecloud-provider");
        return  this.discoveryClient;
    }

}

